// Copyright 2012 Linuxthink. All rights reserved.
// The Word Filter Project
// wf.cpp

#include <iostream>
#include <list>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;

// 1 Read Configuration file, Input file, Dictionary file
// 2 Split word
// 3 Change to lower case
// 4 Delete duplicated word
// 5 Look up dictionary
// 6 Sort
int main() {
  // Variables
  string input_file, output_file, dictionary_file, valid_word;
  bool will_sort = 0;
  list<string> word_list;
  list<string> dictionary_list;
      
  // Functions
  void PrintWelcome();
  void PrintExit();
  bool ReadConfiguration(string &, string &,
                         string &, string &, bool &);
  bool ReadInput(const string input_file, list<string> &word_list);
  bool ReadDictionary(const string dictionary_file,
                      list<string> &dictionary_list);
  void SplitWord(const string valid_word, list<string> &word_list);
  void DeleteDuplicate(list<string> &word_list);
  void ChangeToLowerCase(list<string> &word_list);
  void LookUpDictionary(list<string> &dictionary_list, list<string> &word_list);
  bool WriteOutput(const string output_file, list<string> &word_list);
  
  PrintWelcome();
  // Read input files
  if (!ReadConfiguration(input_file, output_file, 
                         dictionary_file, valid_word, will_sort)) {
    return 0;
  }
  if (!ReadInput(input_file, word_list)) {
    return 0;
  }
  if (!ReadDictionary(dictionary_file, dictionary_list)) {
    return 0;
  }
  
  // Split and find valid word
  SplitWord(valid_word, word_list);
  
  // Change to lower case
  ChangeToLowerCase(word_list);
  
  // Delete duplicated word
  DeleteDuplicate(word_list);
  
  // Look up dictionary
  LookUpDictionary(dictionary_list, word_list);
  
  // Sort list if necessary
  if (will_sort) {
    word_list.sort();
  }
  
  // Output file
  if (!WriteOutput(output_file, word_list)) {
    return 0;
  }
  
  PrintExit();
  return 0;
}

